#include "createplayerscreen.h"
#include "ui_createplayerscreen.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include "player.h"

CreatePlayerScreen::CreatePlayerScreen(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CreatePlayerScreen)
{
    ui->setupUi(this);
    PrepareDatabase();

}

CreatePlayerScreen::~CreatePlayerScreen()
{
    delete ui;
}

void CreatePlayerScreen::PrepareDatabase() {

  //  QSqlDatabase appDb = QSqlDatabase::addDatabase("QSQLITE");
  //  appDb.setDatabaseName("/sdcard/basedata/base.db.sqlite");
  //  appDb.open();
    // qDebug() << appDb.lastError().text();

    //QSqlQuery query("CREATE TABLE IF NOT EXISTS players(first varchar(30), middle varchar(30), last varchar(30), jersy int, u_id  INTEGER PRIMARY KEY AUTOINCREMENT");


  //  query.prepare("SELECT * from players");
  //  query.exec();
  //  while(query.next()) {
     //   qDebug() << "Name:" << query.value(0).toString() << " Number:" << query.value(1).toString();
   // }

}

void CreatePlayerScreen::save(){
    Player* playerPtr= new Player();

    //general info
    playerPtr->setFirstName(ui->firstNameInput->text());
    playerPtr->setMiddleName(ui->middleNameInput->text());
    playerPtr->setLastName(ui->lastNameInput->text());
    playerPtr->setJerseyNumber(ui->jerseyNumberInput->value());

    //offensive stats
    GameStatistic* statPtr= new GameStatistic();
    statPtr->setAtBats(ui->atBatsInput->value());
    statPtr->setSingles(ui->singlesInput->value());
    statPtr->setDoubles(ui->doublesInput->value());
    statPtr->setTriples(ui->triplesInput->value());
    statPtr->setHomeRuns(ui->homerunsInput->value());
    statPtr->setGrandSlams(ui->grandSlamInput->value());
    statPtr->setWalks(ui->walksInput->value());
    statPtr->setStrikeouts(ui->strikeoutsInput->value());
    statPtr->setRunsScored(ui->runsScoredInput->value());
    statPtr->setRunsBattedIn(ui->RBIInput->value());
    statPtr->setStolenBases(ui->stolenBasesInput->value());
    playerPtr->getPlayerStats().setGamesPlayed(ui->gamesPlayedInput->value());

    //defensive stats
    statPtr->setErrors(ui->errorsInput->value());

    //pitching stats
    playerPtr->getPlayerStats().setGamesPitched(ui->gamesPitchedInput->value());
    statPtr->setInningsPitched(ui->inningsPitchedInput->value());
    statPtr->setWalksPitched(ui->walksPitched->value());
    statPtr->setEarnedRuns(ui->earnedRunsInput->value());
    statPtr->setStrikeouts(ui->strikeoutsPitchedInput->value());
    statPtr->setWins(ui->winsInput->value());
    statPtr->setLosses(ui->lossesInput->value());
    statPtr->setHitsAllowed(ui->hitsAllowedInput->value());
    statPtr->setAtBatsFaced(ui->AtBatsFacedInput->value());

    playerPtr->getPlayerStats().setPriorHistory(statPtr);
    playerPtr->getPlayerStats().setTotals(statPtr);
    playerPtr->addPlayerToDB();
    close();
}
